package cc.yiueil.service.impl;

import cc.yiueil.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    private static final Map<String, Map<String, Object>> users;

    static {
        users = new HashMap<>();
        HashMap<String, Object> admin = new HashMap<>();
        admin.put("username", "admin");
        admin.put("password", "$2a$10$nOubKDnM2FLOsvFh1WPMGuj9ut06TkMhZ4ts13b14q.Snts5/xnYm");
        admin.put("phone", "13583414606");
        admin.put("mail", "admin@mail.com");
        users.put("admin", admin);
        HashMap<String, Object> guest = new HashMap<>();
        guest.put("username", "guest");
        guest.put("password", "$2a$10$9E0rotFQX0pAk8Epryh/aeaeQdCgFobljcItFrXUENhlUZKR5RHha");
        guest.put("phone", "19970492037");
        guest.put("mail", "user@mail.com");
        users.put("guest", guest);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Map<String, Object> user = users.get(username);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'", username));
        }
        UserDetails myUser = new UserDetails() {
            public String getPhone() {
                return user.get("phone").toString();
            }

            public String getMail() {
                return user.get("mail").toString();
            }

            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return List.of();
            }

            @Override
            public String getPassword() {
                return user.get("password").toString();
            }

            @Override
            public String getUsername() {
                return user.get("username").toString();
            }

            @Override
            public boolean isAccountNonExpired() {
                return true;
            }

            @Override
            public boolean isAccountNonLocked() {
                return true;
            }

            @Override
            public boolean isCredentialsNonExpired() {
                return true;
            }

            @Override
            public boolean isEnabled() {
                return true;
            }
        };
        return myUser;
    }
}
